package com.centaline.androidsalesblog.rx.request;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.api.usercenter.AddReservationApi;
import com.centaline.androidsalesblog.constants.SourceType;
import com.centaline.androidsalesblog.model.responseobject.SimpleUserCenterRo;
import com.centanet.framework.interfaces.IResponseListener;

import rx.Observable;
import rx.Subscriber;

/**
 * 加入约看
 * Created by guilin on 16/3/23.
 */
public class RequestAddReservationOnSubscribe implements Observable.OnSubscribe<SimpleUserCenterRo> {

    private final Class<?> tag;
    private final String postType;
    private final String estateCode;
    private final String estateName;
    private final String postId;

    public RequestAddReservationOnSubscribe(Class<?> tag, String postType, String estateCode, String estateName, String postId) {
        this.tag = tag;
        this.postType = postType;
        this.estateCode = estateCode;
        this.estateName = estateName;
        this.postId = postId;
    }

    @Override
    public void call(final Subscriber<? super SimpleUserCenterRo> subscriber) {
        AddReservationApi addReservationApi = new AddReservationApi(tag, SimpleUserCenterRo.class, new IResponseListener<SimpleUserCenterRo>() {
            @Override
            public void onResponse(SimpleUserCenterRo simpleUserCenterRo) {
                if (!subscriber.isUnsubscribed()) {
                    subscriber.onNext(simpleUserCenterRo);
                }
                subscriber.onCompleted();
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                subscriber.onError(error);
            }
        });
        addReservationApi.setSourceType("s".equalsIgnoreCase(postType) ? SourceType.ERSHOUFANG : SourceType.ZUFANG);
        addReservationApi.setEstateCode(estateCode);
        addReservationApi.setEstateName(estateName);
        addReservationApi.setPostId(postId);
        addReservationApi.startRequest();
    }
}
